package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.Department;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 系统部门
 * TableName : SYS_DEPARTMENT
 * ClassName : com.swlz.dao.DepartmentDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-26 20:28:33
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class DepartmentDAO {

	/**
	 * 添加一个系统部门
	 * 包含所有字段
	 * @param department - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(Department department) throws SQLException {
		if(department.getDeptId()==null) {
			department.setDeptId(DBUtil.nextId());
		}
		department.setCreateTime(new Date());
		department.setVersionNum(0);
		department.setSortNumber(0L);
		return DBUtil.insert(department);
	}
 
	
	/**
	 * 添加一个系统部门
	 * 只包含非null的字段
	 * @param department - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(Department department) throws SQLException {
		if(department.getDeptId()==null) {
			department.setDeptId(DBUtil.nextId());
		}
		department.setCreateTime(new Date());
		department.setVersionNum(0);
		department.setSortNumber(0L);
		return DBUtil.insertSelective(department);
	}
	

	/**
	 * 根据主键更新一个系统部门
	 * 包含所有字段
	 * @param department - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(Department department) throws SQLException {
		department.setUpdateTime(new Date());
		return DBUtil.update(department);
	}
	
	/**
	 * 根据主键更新一个系统部门
	 * 只包含非null的字段
	 * @param department - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(Department department) throws SQLException {
		department.setUpdateTime(new Date());
		return DBUtil.updateSelective(department);
	}
	
	/**
	 * 根据主键删除一个系统部门
	 * @param deptId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long deptId) throws SQLException  {
		return DBUtil.execute("DELETE FROM SYS_DEPARTMENT WHERE DEPT_ID=?", deptId);
	}
	
	/**
	 * 根据主键获取一个系统部门
	 * @param deptId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Department selectById(Long deptId) throws SQLException  {
		return DBUtil.get("SELECT * FROM SYS_DEPARTMENT WHERE DEPT_ID=?", Department.class ,deptId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param deptId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Department selectByModel(Department department)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM SYS_DEPARTMENT WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(department,sql,params);
		return DBUtil.get(sql.toString(), Department.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param deptId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<Department> selectByModel(Department department,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM SYS_DEPARTMENT WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(department,sql,params);
		sql.append(" ORDER BY DEPT_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), Department.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param deptId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(Department department)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM SYS_DEPARTMENT WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(department,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param deptId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(Department department,StringBuffer sql,List<Object> params) {
		if(department==null){
			return;
		}

		if(department.getDeptId()!=null) {
			sql.append(" AND DEPT_ID=?");
			params.add(department.getDeptId());
		}

		if(department.getDeptName()!=null) {
			sql.append(" AND DEPT_NAME=?");
			params.add(department.getDeptName());
		}

		if(department.getDeptParentId()!=null) {
			sql.append(" AND DEPT_PARENT_ID=?");
			params.add(department.getDeptParentId());
		}

		if(department.getDeptUserId()!=null) {
			sql.append(" AND DEPT_USER_ID=?");
			params.add(department.getDeptUserId());
		}

		if(department.getDeleted()!=null) {
			sql.append(" AND DELETED=?");
			params.add(department.getDeleted());
		}

		if(department.getDeleteUser()!=null) {
			sql.append(" AND DELETE_USER=?");
			params.add(department.getDeleteUser());
		}

		if(department.getDeleteTime()!=null) {
			sql.append(" AND DELETE_TIME=?");
			params.add(department.getDeleteTime());
		}

		if(department.getCreateUser()!=null) {
			sql.append(" AND CREATE_USER=?");
			params.add(department.getCreateUser());
		}

		if(department.getCreateTime()!=null) {
			sql.append(" AND CREATE_TIME=?");
			params.add(department.getCreateTime());
		}

		if(department.getVersionNum()!=null) {
			sql.append(" AND VERSION_NUM=?");
			params.add(department.getVersionNum());
		}

		if(department.getUpdateUser()!=null) {
			sql.append(" AND UPDATE_USER=?");
			params.add(department.getUpdateUser());
		}

		if(department.getUpdateTime()!=null) {
			sql.append(" AND UPDATE_TIME=?");
			params.add(department.getUpdateTime());
		}

		if(department.getSortNumber()!=null) {
			sql.append(" AND SORT_NUMBER=?");
			params.add(department.getSortNumber());
		}

	}
}